package com.cheyun.netsalev3.utils

import android.content.Context
import androidx.lifecycle.ViewModel
import com.cheyun.netsalev3.bean.*
import com.cheyun.netsalev3.bean.aftersale.DriveDetail
import com.cheyun.netsalev3.bean.aftersale.MaintenanceDetail
import com.cheyun.netsalev3.bean.drive.DriveListInfo
import com.cheyun.netsalev3.bean.records.Follow
import com.cheyun.netsalev3.bean.records.Reserve
import com.cheyun.netsalev3.dao.AppDatabase
import com.cheyun.netsalev3.repository.*
import com.cheyun.netsalev3.repository.aftersale.*
import com.cheyun.netsalev3.repository.orderquanyi.OrderQuanyiChildRepository
import com.cheyun.netsalev3.repository.orderquanyi.OrderQuanyiRepository
import com.cheyun.netsalev3.repository.orderquanyi.OrderquanyiDetailsRepository
import com.cheyun.netsalev3.viewmodel.*
import com.cheyun.netsalev3.viewmodel.aftersale.*
import com.cheyun.netsalev3.viewmodel.orderlist.OrderDetailsActivityViewModel
import com.cheyun.netsalev3.viewmodel.orderlist.OrderListFragmentViewModel
import com.cheyun.netsalev3.viewmodel.orderquanyi.OrderQuanyiChildViewModel
import com.cheyun.netsalev3.viewmodel.orderquanyi.OrderQuanyiViewModel
import com.cheyun.netsalev3.viewmodel.orderquanyi.OrderquanyiDetailsActivityViewModel
import com.cheyun.netsalev3.widget.MyDialog
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.HashMap

/**
 *用于注入各种活动和片段所需类的静态方法。
 */
object InjectorUtils {
    fun getStockQueryListViewModelFactory(hashMap: HashMap<String, String>): ViewModelFactory {
        var method: () -> ViewModel = {
            StockQueryListViewModel(StockPageRepository(hashMap))
        }
        return ViewModelFactory(method)

    }
    fun getDriveListViewModelFactory(state: Int): ViewModelFactory {
        var method: () -> ViewModel = {
            DriveListViewModel(DriveListPageRepository(state))
        }
        return ViewModelFactory(method)
    }
    fun getAllcatedListViewModelFactory(state: String): ViewModelFactory {
        var method: () -> ViewModel = {
            AllocatedListViewModel(AllocatedListPageRepository(state))
        }
        return ViewModelFactory(method)
    }
    /**
     * 创建CustomerItemFragmentViewModel
     */
    fun getCustomerItemFragmentViewModelFactory(nkeys: ArrayList<String>?, map: Map<String, String> = HashMap(),boolean: Boolean=false): ViewModelFactory? {
        if (nkeys == null) {
            return null
        }
        var method: () -> ViewModel = {
            var list = HashMap<String, HomeCustomerPageRepository>()
            for (item in nkeys) {
                list[item] = HomeCustomerPageRepository(item, map,boolean,false)

            }
            CustomerItemFragmentViewModel(list)
        }
        return ViewModelFactory(method)
    }

    fun getClueDetailsViewModelFactory(tid: Int, nkey: String,come:String,keliudata:Data?,revkey:String): ViewModelFactory? {
        var method: () -> ViewModel = {
            ClueDetailsActivityViewModel(ClueDetailsRepository(tid, nkey,revkey),come,keliudata,revkey)
        }
        return ViewModelFactory(method)
    }

    fun getSwitchingDealersModelFactory(): ViewModelFactory? {
        var method: () -> ViewModel = {
            SwitchingDealersActivityViewModel(SwitchingDealersRepository())
        }
        return ViewModelFactory(method)
    }

    fun getHomeTaskViewModelFactory(): ViewModelFactory? {
        var method: () -> ViewModel = {
            HomeTaskViewModel(HomeTaskYaoyuePageRepository(), HomeTaskDaoDianPageRepository())
        }
        return ViewModelFactory(method)
    }

    fun getCustomerStoreModelFactory(id: Int): ViewModelFactory? {
        var method: () -> ViewModel = {
            CustomerStoreActivityViewModel(CustomerStorePepository(id))
        }
        return ViewModelFactory(method)
    }

    fun getEditCustomStoreViewModelFactory(data: DaoDianDetailsParam): ViewModelFactory? {
        var method: () -> ViewModel = {
            EditCustomStoreActivityViewModel(data, EditCustomStoreRepository())
        }
        return ViewModelFactory(method)
    }

    fun getAddClueActivityViewModelFactory(ed:EditClueParam,come:String,keliudata:Data?): ViewModelFactory? {
        var method: () -> ViewModel = {
            AddClueActivityViewModel(ed,come,keliudata, AddClueActivityRepository())
        }
        return ViewModelFactory(method)
    }

    fun getCustomerTagActivityViewModelFactory(ids:String): ViewModelFactory? {
        var method: () -> ViewModel = {
            CustomerTagActivityViewModel(ids)
        }
        return ViewModelFactory(method)
    }

    fun getAddDriveActivityViewModelFactory(ed:EditDriveParam): ViewModelFactory? {
        var method: () -> ViewModel = {
            AddDriveActivityViewModel(ed,AddDriveActivityRepository())
        }
        return ViewModelFactory(method)
    }
    fun getChoiceDriveTimeActivityViewModelFactory(reserveseries:String,reservespec:String,reserveday:String,
                                                   reserveseriesname:String,reservespecname:String): ViewModelFactory? {
        var method: () -> ViewModel = {
            ChoiceDriveTimeActivityViewModel(reserveseries,reservespec,reserveday,reserveseriesname,reservespecname,AddDriveActivityRepository())
        }
        return ViewModelFactory(method)
    }

    fun getTaskListActivityViewModelFactory(ckey:String,map:HashMap<String, String>): ViewModelFactory? {
        var method: () -> ViewModel = {
            TaskListActivityViewModel(ckey,TaskListActivityRepository(map))
        }
        return ViewModelFactory(method)
    }

    fun getChooseCarViewModelFactory(carBrandParam: CarBrandParam): ViewModelFactory? {
        var method: () -> ViewModel = {
            ChooseCarViewModel(ChooseCarRepository(), carBrandParam)
        }
        return ViewModelFactory(method)
    }

    fun getMarsChooseCarViewModelFactory( carBrandParam: CarBrandParam,drive:Boolean,stime:String,enableClickAll:Boolean): ViewModelFactory? {
        var method: () -> ViewModel = {
            MarsChooseCarViewModel(ChooseCarRepository(), carBrandParam,drive,stime,enableClickAll)
        }
        return ViewModelFactory(method)
    }

    fun getAddSuccessViewModelFactory(data: EditClueParam, clueDetails: ClueDetails,come:String,keliudata:Data?): ViewModelFactory? {
        var method: () -> ViewModel = {
            AddSuccessActivityViewModel(data,clueDetails,come,keliudata)
        }
        return ViewModelFactory(method)
    }

    fun getClueDetailMingXiActivityViewModelFactory(data: ClueDetails, follow: ArrayList<Follow>): ViewModelFactory? {
        var method: () -> ViewModel = {
            ClueDetailMingXiActivityViewModel(data,follow)
        }
        return ViewModelFactory(method)
    }

    fun getFollowUpViewModelFactory(data: ArrayList<Follow>): ViewModelFactory? {
        var method: () -> ViewModel = {
            FollowUpListActivityViewModel(data)
        }
        return ViewModelFactory(method)
    }

    fun getCarBuyDetailsActivityViewModelFactory(data: ClueDetails, dingCar: Boolean): ViewModelFactory? {
        var method: () -> ViewModel = {
            CarBuyDetailsActivityViewModel(data,dingCar)
        }
        return ViewModelFactory(method)
    }

    fun getEditCarBuyingRecordViewModelFactory(data: ClueDetails, dingCar: Boolean): ViewModelFactory? {
        var method: () -> ViewModel = {
            EditCarBuyingRecordActivityViewModel(data,ClueFollowUpRepository(),dingCar)
        }
        return ViewModelFactory(method)
    }

    fun getClueFollowUpActivityViewModelFactory(data: ClueDetails, leve: String,come:String,keliudata:Data?,isNew:Boolean,
                                                waitAddData:EditClueParam,levelLock:Boolean,canceldrive:Int): ViewModelFactory? {
        var method: () -> ViewModel = {
            ClueFollowUpActivityViewModel(data, leve, ClueFollowUpRepository(),come,keliudata,isNew,waitAddData,levelLock,canceldrive)
        }
        return ViewModelFactory(method)
    }

    fun getMyDialogViewModelFactory(title: String, list: ArrayList<DialogParam>,isMultiple:Boolean = false): ViewModelFactory? {

        var method: () -> ViewModel = {
            MyDialogViewModel(title, list,isMultiple)
        }
        return ViewModelFactory(method)
    }

    fun getChoiceCustomerActivityViewModelFactory(phone:String,tid:String): ViewModelFactory? {
        var method: () -> ViewModel = {
            ChoiceCustomerActivityViewModel(DaoDianListPageRepository(phone,tid))
        }
        return ViewModelFactory(method)
    }

    fun getHomeTaskFragment2ViewModelFactory(): ViewModelFactory? {
        var method: () -> ViewModel = {
            HomeTaskFragment2ViewModel(HomeTaskFragment2Repository())
        }
        return ViewModelFactory(method)
    }

    fun getOverdueActivityViewModelFactory(state: Int): ViewModelFactory? {
        var method: () -> ViewModel = {
            when (state) {
                //逾期state：1 七日强转2
                1 -> {
                    var dateFormat = SimpleDateFormat("yyyy-MM-dd")
                    val c = Calendar.getInstance()
                    var date = dateFormat.format(c.timeInMillis)
                    OverdueActivityViewModel(OverdueActivityPageRepository(date))
                }
                else -> {
                    OverdueActivityViewModel(ForcelistPageRepository())
                }
            }

        }
        return ViewModelFactory(method)
    }

    fun getMissedTelActivityViewModelFactory(): ViewModelFactory? {
        var method: () -> ViewModel = {
            MissedTelActivityViewModel(MissedTellistPageRepository())
        }
        return ViewModelFactory(method)
    }

    fun getHomeAnalysisFragmentViewModelFactory(): ViewModelFactory? {
        var method: () -> ViewModel = {
            HomeAnalysisFragmentViewModel(HomeAnalysisFragmentRepository())
        }
        return ViewModelFactory(method)
    }

    fun getHomeTaskFragment1ViewModelFactory(): ViewModelFactory? {
        var method: () -> ViewModel = {
            HomeTaskFragment1ViewModel(HomeTaskFragment1Repository())
        }
        return ViewModelFactory(method)
    }

    fun getCuesAllocationActivityViewModelFactory(): ViewModelFactory? {
        var method: () -> ViewModel = {
            CuesAllocationActivityViewModel(CuesAllocationPageRepository())
        }
        return ViewModelFactory(method)
    }

    fun getDefeatAuditActivityViewMoldelFactory(nkeys: Array<String>?): ViewModelFactory? {
        if (nkeys == null) {
            return null
        }
        var method: () -> ViewModel = {
            var list = HashMap<String, HomeCustomerPageRepository>()
            for (item in nkeys) {
                list[item] = HomeCustomerPageRepository(item)
            }
            DefeatAuditActivityViewMoldel(list)
        }


        return ViewModelFactory(method)
    }

    fun getAdvancedSearchActivityViewModelFactory(nkey: String): ViewModelFactory? {
        var method: () -> ViewModel = {
            AdvancedSearchActivityViewModel(nkey)
        }
        return ViewModelFactory(method)

    }

    fun getPassengerReceptionViewModelFactory(stime:String,etime:String): ViewModelFactory? {
        var method: () -> ViewModel = {
            PassengerReceptionActivityViewModel(PassengerReceptionPageRepository(stime,etime))
        }
        return ViewModelFactory(method)
    }

    fun getSearchActivityViewModelFactory(nkey: String,from:String,context: Context): ViewModelFactory? {

        var method: () -> ViewModel = {
          var database=  AppDatabase.getInstance(context).getSearchDao()
            SearchActivityViewModel(nkey,from,SearchRepository(database))
        }
        return ViewModelFactory(method)
    }

    fun getCueTransferActivityFactory(nkey: String, ids: String,level:String): ViewModelFactory? {
        var method: () -> ViewModel = {
            CueTransferActivityViewModel(CueTransferActivityRepository(nkey, ids,level))
        }
        return ViewModelFactory(method)
    }

    fun getStockDetailsViewModelFactory(id: Int): ViewModelFactory? {
        var method: () -> ViewModel = {
            StockDetailsActivityViewModel(StockDetailsRepository(id))
        }
        return ViewModelFactory(method)

    }

    fun getWorkSwitchViewModelFactory(): ViewModelFactory? {
        var method: () -> ViewModel = {
            WorkSwitchActivityViewModel(WorkSwitchRepository())
        }
        return ViewModelFactory(method)

    }

    fun getNewsSetActivityViewModelFactory():ViewModelFactory? {
        var method: () -> ViewModel = {
            NewsSetActivityViewModel(NewsSetRepository())
        }
        return ViewModelFactory(method)
    }

    fun getReceptionActivityViewModelFactory(id: String): ViewModelFactory? {
        var method: () -> ViewModel = {
            ReceptionActivityViewModel(id,ReceptionRepository())
        }
        return ViewModelFactory(method)
    }

    fun getCallRecordActivityViewModelFactory(phone: String): ViewModelFactory? {

        var method: () -> ViewModel = {
            CallRecordActivityViewModel(CallRecordRepository(phone))
        }
        return ViewModelFactory(method)
    }

    fun getMessageListActivityViewModelFactory(): ViewModelFactory? {
        var method: () -> ViewModel = {
            MessageListActivityViewModel(MessageListRepository())
        }
        return ViewModelFactory(method)
    }

    fun getChooseCityActivityViewModelFactory(): ViewModelFactory? {
        var method: () -> ViewModel = {
            ChooseCityActivityViewModel(ChooseCityRepository())
        }
        return ViewModelFactory(method)

    }

    fun getWelComeActivityViewModelFactory(): ViewModelFactory? {
        var method: () -> ViewModel = {
            WelComeActivityViewModel()
        }
        return ViewModelFactory(method)
    }

    fun getCallPopFragmentViewModelFactory(url: String, starttime: String, timesName: String): ViewModelFactory? {
        var method: () -> ViewModel = {
            CallPopFragmentViewModel(url,starttime,timesName)
        }
        return ViewModelFactory(method)
    }

    //DM系统
    fun getDmListViewModelFactory(planType:Int,dealed:Int,searchKey:String,status:Int): ViewModelFactory {
        var method: () -> ViewModel = {
            DmListViewModel(DmPageRepository(planType,dealed,searchKey,status))
        }
        return ViewModelFactory(method)
    }

    fun getDmXktViewModelFactory(planType:Int,dealed:Int,searchKey:String, status:ArrayList<Int>?): ViewModelFactory? {
        if(status == null){
            return null
        }
        var method: () -> ViewModel = {
            var list = HashMap<Int, DmPageRepository>()
            for (item in status) {
                list[item] = DmPageRepository(planType,dealed,searchKey,item)
            }
            DmXktViewModel(list)
        }
        return ViewModelFactory(method)
    }

    fun getDmZhtViewModelFactory(planType:Int,dealed:Int,searchKey:String, status:ArrayList<Int>?): ViewModelFactory? {
        if(status == null){
            return null
        }
        var method: () -> ViewModel = {
            var list = HashMap<Int, DmPageRepository>()
            for (item in status) {
                list[item] = DmPageRepository(planType,dealed,searchKey,item)
            }
            DmZhtViewModel(list)
        }
        return ViewModelFactory(method)
    }

    fun getDmCztViewModelFactory(planType:Int,dealed:Int,searchKey:String, status:ArrayList<Int>?): ViewModelFactory? {
        if(status == null){
            return null
        }
        var method: () -> ViewModel = {
            var list = HashMap<Int, DmPageRepository>()
            for (item in status) {
                list[item] = DmPageRepository(planType,dealed,searchKey,item)
            }
            DmCztViewModel(list)
        }
        return ViewModelFactory(method)
    }

    fun getDmSearchActivityViewModelFactory(context: Context): ViewModelFactory? {
        var method: () -> ViewModel = {
            var database=  AppDatabase.getInstance(context).getDmSearchDao()
            DmSearchActivityViewModel(DmSearchRepository(database))
        }
        return ViewModelFactory(method)
    }

    fun getDmClueDetailsViewModelFactory(id:String): ViewModelFactory? {
        var method: () -> ViewModel = {
            DmClueDetailsActivityViewModel(DmClueDetailsRepository(id))
        }
        return ViewModelFactory(method)
    }

    fun getDmClueTransferActivityFactory(clueId: String): ViewModelFactory? {
        var method: () -> ViewModel = {
            DmClueTransferActivityViewModel(DmClueTransferActivityRepository(clueId))
        }
        return ViewModelFactory(method)
    }

    fun getDmClueFollowActivityFactory(clueId: String,type:String,status:String,isArrive:String): ViewModelFactory? {
        var method: () -> ViewModel = {
            DmClueFollowActivityViewModel(DmClueTransferActivityRepository(clueId),type,status,isArrive)
        }
        return ViewModelFactory(method)
    }

    fun getDmSwitchingDealersModelFactory(): ViewModelFactory? {
        var method: () -> ViewModel = {
            DmSwitchingDealersActivityViewModel(DmSwitchingDealersRepository())
        }
        return ViewModelFactory(method)
    }

    fun getDmCallRecordModelFactory(id:Int): ViewModelFactory? {
        var method: () -> ViewModel = {
            DmCallRecordActivityViewModel(DmCallRecordRepository(id))
        }
        return ViewModelFactory(method)
    }

    fun getDmMainViewModelFactory(): ViewModelFactory? {
        var method: () -> ViewModel = {
            DmMainViewModel()
        }
        return ViewModelFactory(method)
    }

    fun getDmChatViewModelFactory(): ViewModelFactory? {
        var method: () -> ViewModel = {
            DmChatViewModel(DmChatRepository())
        }
        return ViewModelFactory(method)
    }

    fun getCarDriveDetailsActivityViewModelFactory(data: ArrayList<Reserve>): ViewModelFactory? {
        var method: () -> ViewModel = {
            CarDriveDetailsActivityViewModel(data)
        }
        return ViewModelFactory(method)
    }

    fun getCarDriveDetailsActivity2ViewModelFactory(id: Int): ViewModelFactory? {
        var method: () -> ViewModel = {
            CarDriveDetailsActivity2ViewModel(id)
        }
        return ViewModelFactory(method)
    }


    fun getOrderListViewModelFactory(nkeys: ArrayList<String>?, map: Map<String, String> = HashMap()): ViewModelFactory? {
        if (nkeys == null) {
            return null
        }
        var method: () -> ViewModel = {
            var list = HashMap<String, OrderListRepository>()
            for (item in nkeys) {
                list[item] = OrderListRepository(item, map,false)

            }
            OrderListFragmentViewModel(list)
        }
        return ViewModelFactory(method)
    }

    fun getOrderDetailsActivityViewModelFactory(tid:Int,id:Int): ViewModelFactory? {
        var method: () -> ViewModel = {
            OrderDetailsActivityViewModel(OrderDetailsRepository(tid,id))
        }
        return ViewModelFactory(method)
    }

    fun getChoiceOrderCustomerActivityViewModelFactory(phone:String,rcvuid:String,tid:String): ViewModelFactory? {
        var method: () -> ViewModel = {
            ChoiceOrderCustomerActivityViewModel(OrderDaoDianListPageRepository(phone,rcvuid,tid))
        }
        return ViewModelFactory(method)
    }

    fun getChoiceOrderActivityViewModelFactory(phone:String): ViewModelFactory? {
        var method: () -> ViewModel = {
            ChoiceOrderActivityViewModel(OrderChooseListPageRepository(phone))
        }
        return ViewModelFactory(method)
    }

    fun getAfterSaleMaintenanceListViewModelFactory(nkeys: ArrayList<String>?, map: Map<String, String> = HashMap(),boolean: Boolean=false): ViewModelFactory? {
        if (nkeys == null) {
            return null
        }
        var method: () -> ViewModel = {
            var list = HashMap<String, MaintenanceRepository>()
            for (item in nkeys) {
                list[item] = MaintenanceRepository(item, map,boolean,false)

            }
            AfterSaleMaintenanceViewModel(list)
        }
        return ViewModelFactory(method)
    }

    fun getMaintenanceDetailsActivityViewModelFactory(id:Int): ViewModelFactory? {
        var method: () -> ViewModel = {
            MaintenanceDetailsActivityViewModel(MaintenanceDetailsRepository(id))
        }
        return ViewModelFactory(method)
    }

    fun getEditMaintenanceViewModelFactory(data: MaintenanceDetail): ViewModelFactory? {
        var method: () -> ViewModel = {
            EditMaintenanceActivityViewModel(data, EditMaintenanceRepository())
        }
        return ViewModelFactory(method)
    }

    fun getChooseCarOwnerActivityViewModelFactory(): ViewModelFactory? {
        var method: () -> ViewModel = {
            ChooseCarOwnerActivityViewModel(CarOwnerRepository())
        }
        return ViewModelFactory(method)
    }

    fun getAfterSaleDriveListViewModelFactory(nkeys: ArrayList<String>?, map: Map<String, String> = HashMap(),boolean: Boolean=false): ViewModelFactory? {
        if (nkeys == null) {
            return null
        }
        var method: () -> ViewModel = {
            var list = HashMap<String, DriveRepository>()
            for (item in nkeys) {
                list[item] = DriveRepository(item, map,boolean,false)

            }
            AfterSaleDriveViewModel(list)
        }
        return ViewModelFactory(method)
    }

    fun getDriveDetailsActivityViewModelFactory(id:Int): ViewModelFactory? {
        var method: () -> ViewModel = {
            DriveDetailsActivityViewModel(DriveDetailsRepository(id))
        }
        return ViewModelFactory(method)
    }

    fun getEditDriveViewModelFactory(data: DriveDetail): ViewModelFactory? {
        var method: () -> ViewModel = {
            EditDriveActivityViewModel(data, EditDriveRepository())
        }
        return ViewModelFactory(method)
    }



    fun getOrderQuanyiListViewModelFactory(nkeys: ArrayList<String>?, map: Map<String, String> = HashMap(),boolean: Boolean=false): ViewModelFactory? {
        if (nkeys == null) {
            return null
        }
        var method: () -> ViewModel = {
            var list = HashMap<String, OrderQuanyiRepository>()
            for (item in nkeys) {
                list[item] = OrderQuanyiRepository(item, map,boolean,false)

            }
            OrderQuanyiViewModel(list)
        }
        return ViewModelFactory(method)
    }

    fun getOrderQuanyilistListViewModelFactory(orderId:String,nkeys: ArrayList<String>?, map: Map<String, String> = HashMap(),boolean: Boolean=false): ViewModelFactory? {
        if (nkeys == null) {
            return null
        }
        var method: () -> ViewModel = {
            var list = HashMap<String, OrderQuanyiChildRepository>()
            for (item in nkeys) {
                list[item] = OrderQuanyiChildRepository(orderId,item, map,boolean,false)

            }
            OrderQuanyiChildViewModel(list)
        }
        return ViewModelFactory(method)
    }

    fun getOrderquanyiDetailsActivityViewModelFactory(id:Int): ViewModelFactory? {
        var method: () -> ViewModel = {
            OrderquanyiDetailsActivityViewModel(OrderquanyiDetailsRepository(id))
        }
        return ViewModelFactory(method)
    }

    fun getFilterActivityViewModelFactory(): ViewModelFactory? {
        var method: () -> ViewModel = {
            FilterActivityViewModel(FilterActivityRepository())
        }
        return ViewModelFactory(method)
    }
}
